package it.univaq.f4i.iw.ex.album.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author Giuseppe Della Penna
 */
public class ImageHelpers {

    public static final int VERTICAL = 0;
    public static final int HORIZONTAL = 1;

    public static Image getThumbnail(Image image, int size, int dir) {
        Image thumb;
        if (dir == HORIZONTAL) {
            thumb = image.getScaledInstance(size, -1, Image.SCALE_SMOOTH);
        } else {
            thumb = image.getScaledInstance(-1, size, Image.SCALE_SMOOTH);
        }
        return thumb;
    }

    public static boolean saveThumbnail(Image image, int size, int dir, File output) {
        ImageIcon thumb = new ImageIcon(getThumbnail(image, size, dir));
        BufferedImage bi = new BufferedImage(thumb.getIconWidth(), thumb.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.drawImage(thumb.getImage(), 0, 0, null);
        try {
            return ImageIO.write(bi, "jpeg", output);
        } catch (IOException ioe) {
            //
        }
        return false;
    }
}
